/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.coraci.anag.gui.components.basic.gui.generic;

import it.coraci.anag.gui.components.basic.gui.logic.CRUDCentral;
import it.coraci.anag.gui.components.basic.gui.logic.CRUDUpdateEntityListener;
import java.util.List;

/**
 *
 * @author Luca
 */
public class GenericOverviewPanel<T> extends javax.swing.JPanel implements CRUDUpdateEntityListener{

    private GenericListModel<T> listModel = new GenericListModel<>();
    private GenericListRenderer<T> listRenderer = new GenericListRenderer<>();
    private Class<T> clazz;
    /**
     * Creates new form PeopleOverview
     */
    public GenericOverviewPanel() {
        initComponents();
        
        this.jList_peopleList.setModel(listModel);
        
        this.jList_peopleList.setCellRenderer(listRenderer);
//        if (!Beans.isDesignTime()) {
//            CRUDCentral.getInstance().addFacade(Person.class, new PersonJpaController(Persistence.createEntityManagerFactory("dbAnagBasePU")));
//            cRUDPanel1.prepareForClass(Person.class);
//            CRUDCentral.getInstance().addCRUDUpdateEntityListener(this);
//            List<T> findAllEntities = CRUDCentral.getInstance().findAllEntities(clazz);
//            PersonJpaController cc = new PersonJpaController(Persistence.createEntityManagerFactory("dbAnagBasePU"));
//            List<Person> findPersonEntities = cc.findPersonEntities();
//            for (Person person : findPersonEntities) {
//                this.listModel.addElement(person);
//            }
//
//        }
    }
    
    public void prepareForClass(Class<T> clazz) {
        this.clazz = clazz;
        cRUDPanel1.prepareForClass(clazz);
        CRUDCentral.getInstance().addCRUDUpdateEntityListener(this);
        List<T> findAllEntities = CRUDCentral.getInstance().findAllEntities(clazz);
        for (T entity : findAllEntities) {
            this.listModel.addElement(entity);
        }
    }

    public GenericListModel<T> getPeopleListModel() {
        return listModel;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList_peopleList = new javax.swing.JList();
        cRUDPanel1 = new it.coraci.anag.gui.components.basic.gui.CRUDPanel();

        jList_peopleList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList_peopleListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList_peopleList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cRUDPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
            .addComponent(cRUDPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jList_peopleListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList_peopleListValueChanged
        T p = (T) this.jList_peopleList.getSelectedValue();
        if (p != null) {
            this.cRUDPanel1.loadEntity(p);
            CRUDCentral.getInstance().entitySelected(p);
        }
    }//GEN-LAST:event_jList_peopleListValueChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private it.coraci.anag.gui.components.basic.gui.CRUDPanel cRUDPanel1;
    private javax.swing.JList jList_peopleList;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void updateThisEntity(Class<?> entityClazz, Object id) {
        System.out.println("update order received on people overview2 panel");
        System.out.println("clazz = "+entityClazz);
        if(clazz.getCanonicalName().equals(entityClazz.getCanonicalName())){
            System.out.println("i have to update entity in list with id: "+id);
            Object findEntity = CRUDCentral.getInstance().findEntity(entityClazz, id);
            this.listModel.updateEntity((T)findEntity);
        }
    }

    @Override
    public void entitySelected(Object entity) {
        
    }

    @Override
    public void newEntity(Object entityClazz) {
        if (clazz.getCanonicalName().equals(entityClazz.getClass().getCanonicalName())) {
            this.listModel.clear();
            List<T> findAllEntities = CRUDCentral.getInstance().findAllEntities(clazz);
            for (T entity : findAllEntities) {
                this.listModel.addElement(entity);
            }
        }
    }
}
